<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="### 动态更换图标 首先先解决下实际问题。动态更换图标。android可以根据activity-alias来设置桌面可以存在多个icon，那直接通过设置某个activity-alias显示其他隐藏即可实现。下面上代码  在xml添加多个activity-alias、删除MainActivity标签的 &lt;category android:name&#x3D;&quot;android.intent.">
<meta property="og:type" content="article">
<meta property="og:title" content="动态更换应用图标：activity-alias详解及应用">
<meta property="og:url" content="http://vchao.top/2017/05/12/%E5%8A%A8%E6%80%81%E6%9B%B4%E6%8D%A2%E5%BA%94%E7%94%A8%E5%9B%BE%E6%A0%87%EF%BC%9Aactivity-alias%E8%AF%A6%E8%A7%A3%E5%8F%8A%E5%BA%94%E7%94%A8/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="### 动态更换图标 首先先解决下实际问题。动态更换图标。android可以根据activity-alias来设置桌面可以存在多个icon，那直接通过设置某个activity-alias显示其他隐藏即可实现。下面上代码  在xml添加多个activity-alias、删除MainActivity标签的 &lt;category android:name&#x3D;&quot;android.intent.">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2017-05-12T12:03:39.000Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.799Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://vchao.top/2017/05/12/%E5%8A%A8%E6%80%81%E6%9B%B4%E6%8D%A2%E5%BA%94%E7%94%A8%E5%9B%BE%E6%A0%87%EF%BC%9Aactivity-alias%E8%AF%A6%E8%A7%A3%E5%8F%8A%E5%BA%94%E7%94%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>动态更换应用图标：activity-alias详解及应用 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2017/05/12/%E5%8A%A8%E6%80%81%E6%9B%B4%E6%8D%A2%E5%BA%94%E7%94%A8%E5%9B%BE%E6%A0%87%EF%BC%9Aactivity-alias%E8%AF%A6%E8%A7%A3%E5%8F%8A%E5%BA%94%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          动态更换应用图标：activity-alias详解及应用
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2017-05-12 20:03:39" itemprop="dateCreated datePublished" datetime="2017-05-12T20:03:39+08:00">2017-05-12</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-08-25 22:37:20" itemprop="dateModified" datetime="2021-08-25T22:37:20+08:00">2021-08-25</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>﻿### 动态更换图标</p>
<p>首先先解决下实际问题。动态更换图标。android可以根据activity-alias来设置桌面可以存在多个icon，那直接通过设置某个activity-alias显示其他隐藏即可实现。下面上代码</p>
<ol>
<li>在xml添加多个activity-alias、删除MainActivity标签的 <code>&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;</code>属性。</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">activity-alias</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:name</span>=<span class="string">&quot;.icon_tag&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:enabled</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:icon</span>=<span class="string">&quot;@mipmap/ic_launcher&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:label</span>=<span class="string">&quot;111111&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:targetActivity</span>=<span class="string">&quot;.MainActivity&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">              <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.action.MAIN&quot;</span> /&gt;</span></span><br><span class="line">              <span class="tag">&lt;<span class="name">category</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.category.LAUNCHER&quot;</span> /&gt;</span></span><br><span class="line">          <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">activity-alias</span>&gt;</span></span><br><span class="line"></span><br><span class="line">      <span class="tag">&lt;<span class="name">activity-alias</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:name</span>=<span class="string">&quot;.icon_tag_1212&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:enabled</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:icon</span>=<span class="string">&quot;@mipmap/head&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:label</span>=<span class="string">&quot;222222&quot;</span></span></span><br><span class="line"><span class="tag">          <span class="attr">android:targetActivity</span>=<span class="string">&quot;.MainActivity&quot;</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">              <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.action.MAIN&quot;</span> /&gt;</span></span><br><span class="line">              <span class="tag">&lt;<span class="name">category</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.category.LAUNCHER&quot;</span> /&gt;</span></span><br><span class="line">          <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">activity-alias</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>如上图 指向的targetActivity可以是不同的，这样启动的activity也是不是一样的，主要的是name要和代码中保持一致</p>
<p>2、代码中控制活动和普通图标的切换</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> useCode =1、为活动图标 =2 为用普通图标 =3、不启用判断</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">   <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">switchIcon</span><span class="params">(<span class="keyword">int</span> useCode)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">try</span> &#123;</span><br><span class="line">           <span class="comment">//要跟manifest的activity-alias 的name保持一致</span></span><br><span class="line">           String icon_tag = <span class="string">&quot;top.vchao.icon.icon_tag&quot;</span>;</span><br><span class="line">           String icon_tag_1212 = <span class="string">&quot;top.vchao.icon.icon_tag_1212&quot;</span>;</span><br><span class="line"></span><br><span class="line">           <span class="keyword">if</span> (useCode != <span class="number">3</span>) &#123;</span><br><span class="line"></span><br><span class="line">               PackageManager pm = getPackageManager();</span><br><span class="line"></span><br><span class="line">               ComponentName normalComponentName = <span class="keyword">new</span> ComponentName(</span><br><span class="line">                       getBaseContext(),</span><br><span class="line">                       icon_tag);</span><br><span class="line">               <span class="comment">//正常图标新状态</span></span><br><span class="line">               <span class="keyword">int</span> normalNewState = useCode == <span class="number">2</span> ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED</span><br><span class="line">                       : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;</span><br><span class="line">               <span class="keyword">if</span> (pm.getComponentEnabledSetting(normalComponentName) != normalNewState) &#123;<span class="comment">//新状态跟当前状态不一样才执行</span></span><br><span class="line">                   pm.setComponentEnabledSetting(</span><br><span class="line">                           normalComponentName,</span><br><span class="line">                           normalNewState,</span><br><span class="line">                           PackageManager.DONT_KILL_APP);</span><br><span class="line">               &#125;</span><br><span class="line"></span><br><span class="line">               ComponentName actComponentName = <span class="keyword">new</span> ComponentName(</span><br><span class="line">                       getBaseContext(),</span><br><span class="line">                       icon_tag_1212);</span><br><span class="line">               <span class="comment">//正常图标新状态</span></span><br><span class="line">               <span class="keyword">int</span> actNewState = useCode == <span class="number">1</span> ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED</span><br><span class="line">                       : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;</span><br><span class="line">               <span class="keyword">if</span> (pm.getComponentEnabledSetting(actComponentName) != actNewState) &#123;<span class="comment">//新状态跟当前状态不一样才执行</span></span><br><span class="line"></span><br><span class="line">                   pm.setComponentEnabledSetting(</span><br><span class="line">                           actComponentName,</span><br><span class="line">                           actNewState,</span><br><span class="line">                           PackageManager.DONT_KILL_APP);</span><br><span class="line">               &#125;</span><br><span class="line"></span><br><span class="line">           &#125;</span><br><span class="line">       &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>上图的icon_tag和icon_tag_1212要和你xml中的activity-alias的name保持一致</p>
<p>补坑。。<strong>activity-alias的name是不能改的!!</strong><br>如果当前版本的是icon_tag_1212，下版本改成icon_tag_act，就会导致系统找icon_tag_1212找不到，导致桌面无图标，应用打不开！！！此坑严重请注意</p>
<h3 id="activity-alias标签元素"><a href="#activity-alias标签元素" class="headerlink" title="activity-alias标签元素"></a>activity-alias标签元素</h3><p>实现了功能，下面就来看一下原理。所周知，AndroidManifest是一个xml文件，它包含很多标签元素，如application、activity、receiver等，其中有一个叫做activity-alias，因为该标签平时很少用到，可能大家对这个标签还不是很熟悉。</p>
<p>activity-alias，顾名思义，即activity的别名。看到这里，大家会想那它究竟是哪个Activity的别名呢？我们在创建一个Activity时，必须在AndroidManifest中静态声明该Activity，同时配置android:name、android:label、android:icon等属性，还可配置intent-filter。对于activity-alias标签，它有一个属性叫android:targetActivity，这个属性就是用来为该标签设置目标Activity的，或者说它就是这个目标Activity的别名。至此我们已经明白activity-alias并非代表一个独立的Activity，而是为一个已经存在的Activity创建的别名。</p>
<h3 id="activity-alias功能"><a href="#activity-alias功能" class="headerlink" title="activity-alias功能"></a>activity-alias功能</h3><p>知道了activity-alias的概念，那么它的功能是什么呢？activity-alias作为一个已存在Activity的别名，则应该可以通过该别名标签声明快速打开目标Activity。因此activity-alias可用来设置某个Activity的快捷入口，可以放在桌面上或者通过该别名被其他组件快速调起。该标签元素支持一些属性及intent-filter、meta-data等配置，因此可以触发一些跟目标Activity不同的功能逻辑，虽然打开的是同一个Activity。举个简单的例子，如之前需要先打开主界面，然后才能点击进入某个Activity，如果使用activity-alias为该Activity配置一个快捷入口，甚至可以为其在桌面生成一个图标，然后点击桌面图标可直接进入该Activity，该功能可满足某些需要快速到达功能界面的需求。</p>
<h3 id="activity-alias语法及声明"><a href="#activity-alias语法及声明" class="headerlink" title="activity-alias语法及声明"></a>activity-alias语法及声明</h3><p>其基本语法如下</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">activity-alias</span> <span class="attr">android:enabled</span>=<span class="string">[</span>&quot;<span class="attr">true</span>&quot; | &quot;<span class="attr">false</span>&quot;]</span></span><br><span class="line"><span class="tag">                <span class="attr">android:exported</span>=<span class="string">[</span>&quot;<span class="attr">true</span>&quot; | &quot;<span class="attr">false</span>&quot;]</span></span><br><span class="line"><span class="tag">                <span class="attr">android:icon</span>=<span class="string">&quot;drawable resource&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:label</span>=<span class="string">&quot;string resource&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:name</span>=<span class="string">&quot;string&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:permission</span>=<span class="string">&quot;string&quot;</span></span></span><br><span class="line"><span class="tag">                <span class="attr">android:targetActivity</span>=<span class="string">&quot;string&quot;</span> &gt;</span></span><br><span class="line">    . . .</span><br><span class="line"><span class="tag">&lt;/<span class="name">activity-alias</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>部分属性说明如下</p>
<ol>
<li> android:enable 该属性用来决定目标Activity可否通过别名被系统实例化，默认为true。需要注意的是application也有enable属性，只用当它们同时为true时，activity-alias的enable才生效。</li>
<li> android:exported 该属性为true的话，则目标Activity可被其他应用调起，如为false则只能被应用自身调起。其默认值根据activity-alias是否包含intent-filter元素决定，如果有的话，则默认为true；没有的话则为false。其实也很好理解，如果有intent-filter，则目标Activity可以匹配隐式Intent，因此可被外部应用唤起；如果没有intent-filter，则目标Activity要被调起的话必须知道其精确类名，因为只有应用本身才知道精确类名，所以此时默认为false。</li>
<li> android:icon 该属性就比较好玩了，允许自定义icon，可以不同于应用本身在桌面的icon。如果需要在桌面上创建快捷入口，也许产品会要求换个不同的icon。</li>
<li> android:label 该属性类似于android:icon，图标都换了，换个名称也合情合理吧，此属性就是为此而生的。</li>
<li> android:name 该属性可以为任意字符串，但最好符合类名命名规范。activity元素的name属性实质上都会指向一个具体的Activity类，而activity-alias的name属性仅作为一个唯一标识而已。</li>
<li> android:permission 该属性指明了通过别名声明调起目标Activity所必需的权限。</li>
<li> android:targetActivity 该属性指定了目标Activity，即通过activity-alias调起的Activity是哪个，此属性其实类似于activity标签中的name属性，需要规范的Activity包名类名。</li>
</ol>
<p>看了以上几个主要属性，大家应该意识到activity-alias的属性是activity属性的子集，如果是activity-alias和activity共有的属性，则以activity-alias为准，目标Activity中的设置并不会在activity-alias中生效；如果是仅activity才有的属性，则为目标Activity配置的属性会在activity-alias中生效。</p>
<p>activity-alias可以设置自己的intent-filter或meta-date，最常用的就是设置如下intent-filter从而在桌面Launcher上创建一个快捷入口：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;intent-filter&gt;</span><br><span class="line">    &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;</span><br><span class="line">    &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;</span><br><span class="line">&lt;/intent-filter&gt;</span><br></pre></td></tr></table></figure>

<p>另外需要注意的一点是，在AndroidManifest配置文件中，activity-alias标签元素必须声明在目标Acitvity对应的activity标签元素之后，否则会编译错误。</p>
<h3 id="activity-alias用法示例"><a href="#activity-alias用法示例" class="headerlink" title="activity-alias用法示例"></a>activity-alias用法示例</h3><p>到此大家已经对activity-alias了解的比较深入了，来看一个activity-alias的经典用法：为某个Activity在桌面创建一个快捷入口。</p>
<p>代码很简单，应用包含两个Activity：MainActivity和SecondActivity，MainActivity有一个Button，点击跳转到SecondActivity；我们为SecondActivity配置一个activity-alias标签元素，在桌面上为其创建一个快捷入口，点击可以直接打开SecondActivity。</p>
<p>该示例应用的AndroidManifest.xml内容如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">manifest</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">package</span>=<span class="string">&quot;me.geed.activityalias&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">application</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:allowBackup</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:icon</span>=<span class="string">&quot;@mipmap/ic_launcher&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:label</span>=<span class="string">&quot;@string/app_name&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:supportsRtl</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">        <span class="attr">android:theme</span>=<span class="string">&quot;@style/AppTheme&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.MainActivity&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.action.MAIN&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">                <span class="tag">&lt;<span class="name">category</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.category.LAUNCHER&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">activity</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity</span> <span class="attr">android:name</span>=<span class="string">&quot;.SecondActivity&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">activity</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">activity-alias</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:name</span>=<span class="string">&quot;SecondActivityAlias&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:label</span>=<span class="string">&quot;Alias快捷入口&quot;</span></span></span><br><span class="line"><span class="tag">            <span class="attr">android:targetActivity</span>=<span class="string">&quot;.SecondActivity&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">action</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.action.MAIN&quot;</span> /&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">category</span> <span class="attr">android:name</span>=<span class="string">&quot;android.intent.category.LAUNCHER&quot;</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">intent-filter</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">activity-alias</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">application</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">manifest</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>将应用部署到手机上，你会发现生成了两个桌面图标（此处由于没有为activity-alias另指定icon，因此两者icon相同），名称分别为“ActivityAlias”和“Alias快捷入口”。</p>
<p>点击“ActivityAlias”应用图标，会启动MainActivity，在MainActivity中点击Button，跳转到SecondActivity。</p>
<p>点击“Alias快捷入口”图标，则可以直接打开SecondActivity，且SecondActivity的title为我们在activity-alias中设置的 label属性名称。</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2017/05/12/%E5%AE%89%E5%8D%93%E5%A4%87%E5%BF%98%EF%BC%9A%E8%B7%B3%E8%BD%AC%E7%B3%BB%E7%BB%9F%E9%A1%B5%E9%9D%A2%E4%BB%A5%E5%8F%8A%E5%B8%B8%E8%A7%81APP%E7%9B%B8%E5%BA%94%E9%A1%B5%E9%9D%A2/" rel="prev" title="安卓备忘：跳转系统页面以及常见APP相应页面">
      <i class="fa fa-chevron-left"></i> 安卓备忘：跳转系统页面以及常见APP相应页面
    </a></div>
      <div class="post-nav-item">
    <a href="/2017/05/12/%E4%BB%8E%E6%BA%90%E7%A0%81%E6%9D%A5%E7%9C%8BgetReadableDatabase()%E7%9A%84%E8%AF%AF%E5%8C%BA/" rel="next" title="从源码来看getReadableDatabase()的误区">
      从源码来看getReadableDatabase()的误区 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#activity-alias%E6%A0%87%E7%AD%BE%E5%85%83%E7%B4%A0"><span class="nav-number">1.</span> <span class="nav-text">activity-alias标签元素</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#activity-alias%E5%8A%9F%E8%83%BD"><span class="nav-number">2.</span> <span class="nav-text">activity-alias功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#activity-alias%E8%AF%AD%E6%B3%95%E5%8F%8A%E5%A3%B0%E6%98%8E"><span class="nav-number">3.</span> <span class="nav-text">activity-alias语法及声明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#activity-alias%E7%94%A8%E6%B3%95%E7%A4%BA%E4%BE%8B"><span class="nav-number">4.</span> <span class="nav-text">activity-alias用法示例</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
